package com.ip;

import com.graphics.ImageIdentity;
import com.lm.Tile;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import mapgenerator.*;

/**
 *
 * @author Sajarwo Anggai
 */
public class IPControlPanel implements ActionListener, MouseListener, ChangeListener, TreeSelectionListener {

    private Main comp;
    private ImageIdentity currentImageIdentity;
    private ImageIdentity currentOnWorkspace;
    private Tile currentTile;
    private String currentImageName;
    private int hGap = 5;
    private int vGap = 5;

    public IPControlPanel(Main comp) {
        this.comp = comp;
        comp.IPButtonRename.addActionListener(this);
        comp.IPButtonRemove.addActionListener(this);
        comp.IPButtonSetOnWorkspace.addActionListener(this);
        comp.IPHGapSlider.addChangeListener(this);
        comp.IPVGapSlider.addChangeListener(this);
    }

    void showImgeManagerInfo(boolean active) {
        comp.IPButtonRename.setVisible(active);
        comp.IPButtonRemove.setVisible(active);
        comp.IPButtonSetOnWorkspace.setVisible(active);
    }

    public void setHGap(int hGap) {
        this.hGap = hGap;
    }

    public void setVGap(int vGap) {
        this.vGap = vGap;
    }

    public int getHGap() {
        return hGap;
    }

    public int getVGap() {
        return vGap;
    }

    public void setCurrentImageIdentity(ImageIdentity currentImageIdentity) {
        this.currentImageIdentity = currentImageIdentity;
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        if (e.getSource() == comp.IPHGapSlider) {
            setHGap(comp.IPHGapSlider.getValue());
            comp.IPHGapSlider.setToolTipText("" + comp.IPHGapSlider.getValue());
            comp.IPHGap.setText("HGap: " + comp.IPHGapSlider.getValue() + "px");
        } else {
            setVGap(comp.IPVGapSlider.getValue());
            comp.IPVGapSlider.setToolTipText("" + comp.IPVGapSlider.getValue());
            comp.IPVGap.setText("VGap: " + comp.IPVGapSlider.getValue() + "px");
        }
        if (!comp.project.isCreated()) {
            return;
        }
        previewPattern(comp.ImagePatternRightPanelRender, currentImageIdentity.getName());
    }

    public void setCurrentImage(String currentImageName) {
        this.currentImageName = currentImageName;
    }

    public String getCurrentImage() {
        return currentImageName;
    }

    public Tile getCurrentTile() {
        if (currentOnWorkspace != null && currentTile != null) {
            System.out.println("Pos = " + currentTile.getPos());
            if (currentOnWorkspace.equals(currentTile.getImageIdentity())) {
                return currentTile;
            }
        }
        return null;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == comp.IPButtonRename) {
            comp.imControlPanel.renameImage(currentImageName);
        } else if (e.getSource() == comp.IPButtonRemove) {
            removeImagePattern();
        } else {
            if (!currentImageIdentity.isSprite()) {
                previewPattern(comp.WorkspaceBottomPattern, currentImageIdentity.getName());
                currentOnWorkspace = currentImageIdentity;
            }
        }
    }

    public void removeImagePattern() {
        if (currentOnWorkspace == currentImageIdentity) {
            comp.WorkspaceBottomPattern.removeAll();
            comp.WorkspaceBottomPattern.repaint();
            comp.WorkspaceBottomPattern.revalidate();
            currentOnWorkspace = null;
        }
        comp.ipContainer.remove(getCurrentImage());
        comp.ipPanelWidget.reloadAll();
        comp.osPanelWidget.reloadAll();
    }

    @Override
    public void valueChanged(TreeSelectionEvent e) {
        if (!comp.project.isCreated()) {
            return;
        }
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) comp.ipPanelWidget.getTree().getLastSelectedPathComponent();
        try {
            if (node != null & !node.isRoot() & node.isLeaf()) {
                ImageIdentity ii = (ImageIdentity) node.getUserObject();
                previewPattern(comp.ImagePatternRightPanelRender, ii.getName());
                showImgeManagerInfo(true);
                if (!node.getParent().toString().equals("Groups Pattern")) {
                    comp.IPButtonSetOnWorkspace.setVisible(false);
                }
                setCurrentImageIdentity(ii);
                setCurrentImage(ii.getName());
            } else {
                showImgeManagerInfo(false);
                comp.ImagePatternRightPanelRender.removeAll();
            }
        } catch (Exception err) {
            showImgeManagerInfo(false);
            comp.ImagePatternRightPanelRender.removeAll();
        }
        comp.ImagePatternRightPanelRender.repaint();
        comp.ImagePatternRightPanelRender.revalidate();
    }

    public void previewPattern(JPanel render, String imageName) {
        ImageIdentity ident = comp.ipContainer.getPattern(imageName);
        if (ident == null) {
            return;
        }
        if (render == comp.ImagePatternRightPanelRender) {
            setCurrentImageIdentity(ident);
        }
        render.removeAll();
        int x = 10, y = 10;
        int width = 0, height = 0;
        for (int i = 0; i < ident.getImages().size(); i++) {
            TilePattern tp = new TilePattern(ident, i);
            tp.setToolTipText("Pos = " + i);
            tp.setVisible(true);
            tp.addMouseListener(this);
            if (ident.isSprite()) {
                width = tp.getImage().getWidth();
                height = tp.getImage().getHeight();
            } else {
                width = comp.project.getTileWidth();
                height = comp.project.getTileHeight();
            }
            render.add(tp).setBounds(x, y, width, height);
            if ((i + 1) % ident.getCols() == 0) {
                x = 10;
                y += height + comp.ipControlPanel.getVGap();
            } else {
                x += width + comp.ipControlPanel.getHGap();
            }
        }
        x = ident.getCols() * width;
        x += ident.getCols() * comp.ipControlPanel.getHGap();
        y = ident.getRows() * height;
        y += ident.getRows() * comp.ipControlPanel.getVGap();

        render.setPreferredSize(new Dimension(x + 20, y + 20));
        render.repaint();
        render.revalidate();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        TilePattern tp = (TilePattern) e.getSource();
        currentTile = new Tile(tp.getImageIdentity(), tp.getPosition());
        System.out.println(currentTile.getImageIdentity().getName() + " SettedPos = " + currentTile.getPos());
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
